package com.lance.answer.util;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class XinJiangUtil {

    /**
     * 教师登录
     *
     * @param driver
     * @param username
     * @param password
     */
    public static void login(WebDriver driver, String username, String password) {
        initLogin(driver, username, password);
        driver.findElement(By.cssSelector("button[class='el-button login-ruleForm-btn el-button--primary']")).click();
    }

    /**
     * 学生登录
     *
     * @param driver
     * @param username
     * @param password
     */
    public static void studentLogin(WebDriver driver, String username, String password) {
        initLogin(driver, username, password);
        driver.findElement(By.cssSelector("button[class='el-button el-button--primary']")).click();
    }

    private static void initLogin(WebDriver driver, String username, String password) {
        for (WebElement we : driver.findElements(By.className("el-input__inner"))) {
            if ("text".equals(we.getAttribute("type"))) {
                we.sendKeys(new String[]{username});
            }
            if ("password".equals(we.getAttribute("type"))) {
                we.sendKeys(new String[]{password});
            }
        }
    }


    // 进入某个课程
    public static void goOn(WebDriver driver, String courseName, String buttonName) {
        boolean isGoOn = true;
        for (WebElement we : driver.findElements(By.cssSelector("div[class='contblockout clearfix box-card-component el-loading-parent--relative'], " +
                "div[class='contblockout clearfix box-card-component']"))) {
            for (WebElement cursor : we.findElements(By.className("cursor"))) {
                if (!"p".equals(cursor.getTagName())) {
                    continue;
                }
                String title = cursor.findElement(By.tagName("span")).getText();
                //  if (!Strings.isNullOrEmpty(title) && title.contains("职业生涯规划")) {
                if (!Strings.isNullOrEmpty(title) && title.contains(courseName)) {
                    for (WebElement goOn : we.findElements(By.className("v-btn__content"))) {
                        if (buttonName.equals(goOn.getText())) {
                            System.err.println("go go go go");
                            goOn.click();
                            isGoOn = false;
                            break;
                        }
                    }
                }
            }
            if (!isGoOn) {
                break;
            }
        }
    }



    // 初始化http头部
    public static Map<String, String> initHttpHeads(String studentSpaceProfile, String authorization) {
        Map<String, String> heads = Maps.newHashMap();
        heads.put("referer", "https://xjskk.open.com.cn/teacher/");
        heads.put("authority", "xjskk.open.com.cn");
      //  heads.put("scheme", "scheme");
        //  heads.put("open-student-space-profile", studentSpaceProfile);
        heads.put("open-teacher-space-profile", studentSpaceProfile);
        heads.put("origin", "https://xjskk.open.com.cn");
        heads.put("authorization", authorization);
        return heads;
    }

    // 初始化http头部
    public static Map<String, String> initStudentHttpHeads(String studentSpaceProfile, String authorization) {
        Map<String, String> heads = Maps.newHashMap();
        heads.put("referer", "https://xjskk.open.com.cn/stuspace/");
        heads.put("authority", "xjskk.open.com.cn");
        heads.put("scheme", "scheme");
          heads.put("open-student-space-profile", studentSpaceProfile);
      //  heads.put("open-teacher-space-profile", studentSpaceProfile);
        heads.put("origin", "https://xjskk.open.com.cn");
        heads.put("authorization", authorization);
        return heads;
    }


    /**
     * 将cookie里面的XSKJ-AdminBasicTest2AndSave-Struct提取出来封装成header里面的参数
     * @param set cookie集合
     * @return studentSpaceProfile
     */
    public static String getStudentSpaceProfile( Set<Cookie> set){
        String studentSpaceProfile = "";
        for (Cookie c : set) {
          //  if ("XSKJ-AdminBasicTest2AndSave-Struct" .equals(c.getName())) {
            if ("XSKJ-Admin-Struct" .equals(c.getName())) {
                studentSpaceProfile = c.getValue();
                studentSpaceProfile = studentSpaceProfile.replaceAll("%25", "%");
                studentSpaceProfile = studentSpaceProfile.replaceAll("%2C", ",");
                System.err.println("studentSpaceProfile:" + studentSpaceProfile);
            }
        }
        return studentSpaceProfile;
    }

    /**
     * 将cookie里面的XXSKJ-ticket提取出来封装成header里面的参数
     * @param set cookie集合
     * @return authorizati
     */
    public static String getAuthorization( Set<Cookie> set){
        String authorizati = "";
        for (Cookie c : set) {
            if("XSKJ-ticket".equals(c.getName())){
                authorizati = "Bearer "+c.getValue();
            }
        }
        return authorizati;
    }


    /**
     * 第一次：保存视频资源文件
     *
     * @param openCourseId openCourseId
     * @param resourceId   resourceId
     * @param videoLength  videoLengthok
     * @return JSONObject
     */
    public static JSONObject saveResourceTrace(int openCourseId, String resourceId, int videoLength) {
        JSONObject saveJson = new JSONObject();
        // 课程ID，固定的
        saveJson.put("openCourseId", openCourseId);
        // 单个视频的资源ID
        saveJson.put("resourceUid", resourceId);
        // 视频总时长
        saveJson.put("totalTime", videoLength);
        saveJson.put("traceTime", 0);
        return saveJson;
    }

    /**
     * 第二次：更新视频观看时间
     *
     * @param openCourseId openCourseId
     * @param resourceId   resourceId
     * @param videoLength  videoLength
     * @param contentId    contentId
     * @return JSONObject
     */
    public static JSONObject updateResourceTrace(int openCourseId, String resourceId, int videoLength, int contentId) {
        JSONObject saveJson = new JSONObject();
        // 课程ID，固定的
        saveJson.put("openCourseId", openCourseId);
        // 单个视频的资源ID
        saveJson.put("resourceUid", resourceId);
        // 视频总时长
        saveJson.put("traceTime", videoLength);
        // 课程的ID
        saveJson.put("id", contentId);


        return saveJson;
    }


}
